home *** CD-ROM | disk | FTP | other *** search
/ JCSM Shareware Collection 1996 September / JCSM Shareware Collection (JCS Distribution) (September 1996).ISO / bother__ / cenvid.zip / CENVIDOS.ZIP / WHICH.BAT < prev    next >
DOS Batch File  |  1994-11-09  |  3KB  |  101 lines

  1. @echo off
  2. REM ********************************************************************
  3. REM *** Which.bat - Show when command would be executed by searching ***
  4. REM *** ver.1       for command along path.                          ***
  5. REM ********************************************************************
  6. CEnviD %0.bat %1 %2
  7. GOTO CENVI_EXIT
  8.  
  9. ExecuteOrder = { ".COM", ".EXE", ".BAT" };
  10.  
  11. Instructions()
  12. {
  13.    printf("\a\n")
  14.    puts(`Which - Determine which command in PATH will execute, and what order`)
  15.    puts(``)
  16.    puts(`SYNTAX: Which <CommandSpec>`)
  17.    puts(``)
  18.    puts(`WHERE: CommandSpec is any file spec.  If CommandSpec has no extension then`)
  19.    printf(`       will search executables in this order:`)
  20.    for ( i = 0; i <= GetArraySpan(ExecuteOrder); i++ )
  21.       printf(" %s",ExecuteOrder[i])
  22.    puts(``)
  23.    puts(`EXAMPLES: Which EDIT`)
  24.    puts(`          Which SESSION.C*`)
  25.    exit(EXIT_FAILURE);
  26. }
  27.  
  28. main(argc,argv)
  29. {
  30.    if ( argc != 2 )   Instructions();
  31.  
  32.    NameParts = SplitFileName(argv[1]);
  33.    if ( NameParts.dir[0] ) {
  34.       printf("\aWHICH whill not work with directory specifications.\n");
  35.       exit(EXIT_FAILURE);
  36.    }
  37.  
  38.    // if extension then use it, else use our extension list
  39.    if ( NameParts.ext[0] ) {
  40.       Extensions[0] = NameParts.ext;
  41.       ExtensionCount = 1;
  42.    } else {
  43.       Extensions = ExecuteOrder;
  44.       ExtensionCount = 1 + GetArraySpan(ExecuteOrder);
  45.    }
  46.  
  47.    // get prioritized list of all directories
  48.    PathCount = BuildPathList(PathList);
  49.  
  50.    // for each directory, find all of the matching files
  51.    FindCount = 0;
  52.    for ( PathIdx = 0; PathIdx < PathCount; PathIdx++ ) {
  53.       for ( ExtensionIdx = 0; ExtensionIdx < ExtensionCount; ExtensionIdx++ ) {
  54.          FindCount += FindFileInDirectory(PathList[PathIdx],NameParts.name,Extensions[ExtensionIdx]);
  55.       }
  56.    }
  57.  
  58.    if ( !FindCount ) {
  59.       printf("\aNo commands found for %s\n",argv[1]);
  60.       exit(EXIT_FAILURE);
  61.    }
  62.  
  63. }
  64.  
  65.  
  66. BuildPathList(List)
  67. {
  68.    Count = 0;
  69.    // First path will always be the current one
  70.    List[Count++] = FullPath(".");
  71.  
  72.    // get full path of each directory from the PATH variable
  73.    if ( Path = getenv("PATH") ) {
  74.       for ( Dir = strtok(Path,";"); Dir; Dir = strtok(NULL,";") ) {
  75.          if ( Dir[0] )
  76.             List[Count++] = FullPath(Dir);
  77.       }
  78.    }
  79.  
  80.    // make sure each element in path list ends in '\'
  81.    for ( i = 0; i < Count; i++ ) {
  82.       if ( strcmp(":\\",List[i]+1) )
  83.          strcat(List[i],"\\");
  84.    }
  85.    return Count;
  86. }
  87.  
  88. FindFileInDirectory(DirSpec,NameSpec,ExtSpec)
  89. {
  90.    sprintf(FindSpec,"%s%s%s",DirSpec,NameSpec,ExtSpec);
  91.    if ( !(FindList = Directory(FindSpec,False,FATTR_RDONLY|FATTR_ARCHIVE)) )
  92.       return 0;
  93.  
  94.    Count = 1 + GetArraySpan(FindList);
  95.    for ( i = 0; i < Count; i++ )
  96.       printf("  %s\n",FindList[i].name);
  97.    return Count;
  98. }
  99.  
  100. :CENVI_EXIT
  101.